<?php
class Group_management extends CI_Controller {
	
	public function __construct() {
        parent::__construct();
        $this->load->model('mgroup_management');
		$this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library('encrypt');
        $this->load->library('enc');
        $this->load->model('mmaster');
        $this->load->model('mglobal');
    }
	
	########################################################################
    ########################################################################

    function index()
    {

		$data['template'] = "shell/smooth";
		$data['main_view'] = 'f-group-management';
		$data['user_group_id'] = '';
		$data['user_group_name'] = '';
		$data['user_group_caption'] = '';
		$data['territory_type_id'] = '';
		$data['istatus'] = '';
		$data['parent_id'] ='';
		$data['having_stock'] ='';
		$data['is_use_top'] = '';


		//untuk kebutuhan update
		if(isset($_GET['update_group_id']))
		{
			$_GET['update_group_id'] = $this->enc->defog($_GET['update_group_id']);
			$result = $this->mgroup_management->get_spesific_group_data($_GET['update_group_id']);
			foreach ($result as $r) 
			{
				$data['user_group_id'] = $r->user_group_id;
				$data['user_group_name'] = $r->user_group_name;
				$data['user_group_caption'] = $r->user_group_caption;
				$data['territory_type_id'] = $r->territory_type_id;
				$data['istatus'] = $r->istatus;
				$data['parent_id'] = $r->parent_id;
				$data['having_stock'] = $r->having_stock;
				$data['is_use_top'] = $r->is_use_top;

				$this->session->set_userdata('user_group_name_before', $r->user_group_name);
				$this->session->set_userdata('user_group_caption_before', $r->user_group_caption);
			}
		}
		$data['user_group_data'] = $this->mgroup_management->get_territory_name();
		$data['parent_group_data'] = $this->mgroup_management->get_parent();
		$this->load->view($data['template'], $data);
    }

    function add()
	{
		$this->form_validation->set_rules('user_group_name', 'Group Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('caption', 'Group Caption', 'required');
		$this->form_validation->set_rules('territory_type_id', 'Territory Type', 'required');

		if ($this->form_validation->run() != FALSE)
		{
			$this->mgroup_management->save();
			$this->mglobal->saveLog('add group', '(added new group: '.$this->session->userdata('group_name').')', $this->session->userdata("username"));

			$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
			redirect(base_url() . "index.php/group_management");		
		}

		redirect(base_url() . "index.php/group_management");

	}

	function update()
	{
		$this->form_validation->set_rules('user_group_name', 'Group Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('caption', 'Group Caption', 'required');
		$this->form_validation->set_rules('territory_type_id', 'Territory Type', 'required');

		if ($this->form_validation->run() != FALSE)
		{
			$this->mgroup_management->update(
				$this->input->post('user_group_id'), 
				$this->input->post('user_group_name'), 
				$this->input->post('caption'),
				$this->input->post('territory_type_id'),
				$this->input->post('parent_id'),
				$this->input->post('istatus'),
				$this->input->post('having_stock'),
				$this->input->post('is_use_top')
				);

			// $this->mglobal->saveLog('update user group', '(Updated group alias name from: '.$this->session->userdata("alias_name_before").' to: '.$this->session->userdata("alias_name_after").' | user group id from: '.$this->session->userdata("user_group_id_before").' to: '.$this->session->userdata("user_group_id_after").')', $this->session->userdata("username"));

			$this->session->set_flashdata('message', 'Data Berhasil diperbaharui');
			redirect(base_url() . "index.php/group_management");		
		}

		redirect(base_url() . "index.php/group_management");
	}

    public function getById($id) {
        if (isset($id))
            echo json_encode($this->mmaster->getAgents($id));
    }

    public function read() {
		//echo json_encode( $this->mgroup_management->getGroup() );
		$result = $this->mgroup_management->getGroup();

		//encode
        foreach ($result['aaData'] as $r => &$value) 
        {
            $value[0] = $this->enc->fogit($value[0]);
        }
        
        echo json_encode($result);
	}
	
	 function defoger()
    {
        $result = $this->enc->defog($this->input->post('str'));
        echo $result;  
    }

	// public function update( $kindOfRecord, $id )
	// {
	// 	$this->magents->update( $id );
	// }
	
	public function delete() {
		// if( is_null( $id ) ) {
		// 	echo 'ERROR: Id not provided.';
		// 	return;
		// }
		$id = $this->uri->segment(3);

		//decode id
		$id = $this->enc->defog($id);
		
		$this->mgroup_management->delete( $id );
		// echo 'Data berhasil dihapus.';
		// return;
		$this->session->set_flashdata('message', 'Data berhasil dihapus');
		
		//redirect(base_url() . "index.php/group_management");
		

	}
	
	
	################
	##function untuk menampilkan view
	
	// function index($proses="",$id="")
	// {
	// 	$data['template'] 	= "shell/smooth";
	// 	$data['users']    	= $this->mgroup_management->get();
	// 	$data['main_view']	= 'f-group-management';
	// 	$data['proses']		= $proses;
	// 	$data['id']			= $id;
		
	// 	$data['data'] = array("name"=>"","parent"=>"","stock"=>"");
	// 	if($proses<>"" && $id<>""){
	// 	    $getdata = $this->mgroup_management->get($id);
	// 	    foreach($getdata as $r){
	// 			$data['data']['parent'] 		= $r->parent_id;
	// 			$data['data']['name'] 		= $r->user_group_name;
	// 			$data['data']['stock'] 		= $r->having_stock;
	// 	    }
	// 	}

	// 	#inisialisasi elemen form yang harus diisi
	// 	$this->form_validation->set_rules('name', 'Group Name', 'required');
		
	// 	$this->form_validation->set_error_delimiters('<li>','</li>');
	// 	$this->form_validation->set_message('required', 'Kolom <b>%s</b> harus diisi.');
                
	// 	if ($this->form_validation->run() != FALSE){
	// 		// masukkan data di sini
	// 		$this->mgroup_management->save($id);
	// 		$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
	// 		redirect(base_url()."index.php/group_management");
	// 	}	
	// 	$hasil = "";
	// 	$this->load->view($data['template'],$data);
	// }
}
?>